package com.lab02.spring.firstpage.controllers;

import com.lab02.spring.firstpage.models.Player;
import com.lab02.spring.firstpage.services.PlayerService;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: bsceran
 * Date: 5/24/13
 * Time: 4:01 PM
 * To change this template use File | Settings | File Templates.
 */
public class PlayerController extends MultiActionController {
    private PlayerService playerService;

    public void setPlayerService(PlayerService playerService) {
        this.playerService = playerService;
    }

    public ModelAndView add(HttpServletRequest request, HttpServletResponse response, Player player) {
        playerService.add(player);
        return new ModelAndView("playerListRedirect");
    }


    public ModelAndView remove(HttpServletRequest request, HttpServletResponse response) throws ServletRequestBindingException {
        String playerName = ServletRequestUtils.getRequiredStringParameter(request,"playerName");
        playerService.remove(playerName);
        return new ModelAndView("playerListRedirect");
    }

    public ModelAndView list(HttpServletRequest request, HttpServletResponse response) throws ServletRequestBindingException {
         List<Player> playerList = playerService.list();
        return new ModelAndView("playerList", "players", playerList);
    }


}
